package code.refactoring.View;
import java.security.InvalidParameterException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Menu {
	private static final int MAX_CHOOSE_OPTION = 3;
	private static final int MIN_CHOOSE_OPTION = 1;
	private int choice;

	/**
	 * Prints Menu options and gets player's choice by getInputChoice() method
	 */
	public void printMenu() {
		do {
			System.out.println("==============================");
			for (int i = 0; i < 5; i++) {
				if (i == 2) {
					System.out.println("|            Menu            |");
					System.out.println("|                            |");
					System.out.println("|    1. Play                 |");
					System.out.println("|    2. Game instructions    |");
					System.out.println("|    3. Exit                 |");
				} else {
					System.out.println("|                            |");
				}
			}
			System.out.println("==============================");
			setChoice(getInputChoice());
		} while (choice < MIN_CHOOSE_OPTION || choice > MAX_CHOOSE_OPTION);
	}

	/**
	 * Prints the Game instructions
	 * 
	 */
	public void printInstructions() {
		System.out.print("The games is played by two people. They take"
				+ " turns at drawing a line between two points.\n");
		System.out
				.print("The line must connect two neighboring"
						+ " points and must not be in diagonal. If a square between four\n");
		System.out
				.print("points is formed, the player that drew the last (closing) line scores a point\n");
		System.out
				.print("and it's his turn again. The game ends when the last possible line is inserted.\n");
		System.out
				.println("The winner is the player with more closed squares.");
	}

	/**
	 * 
	 * @param choice
	 *            - player's choice from getInputChoice() method
	 */
	public void setChoice(int choice) {
		this.choice = choice;
	}

	/**
	 * return's player's choice
	 * 
	 * @return
	 */
	public int getChoice() {
		return this.choice;
	}

	/**
	 * gets inputed choice for menu
	 * 
	 * @return - player's choice
	 */
	private int getInputChoice() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Make your choice: ");
		int choice = 0;
		try {
			choice = sc.nextInt();
		} catch (InputMismatchException ime) {
			System.err.println("Invalid data format!");
		} catch (InvalidParameterException ipe) {
			System.err.println("Invalid paramater!");
		} catch (Exception e) {
			System.err.println("Error occurred!");
		}
		return choice;
	}
}
